package com.zhang.learn.stringleran;

import cn.hutool.core.annotation.AnnotationUtil;
import lombok.Getter;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * @author zhanghaolian
 * @date 2021/6/13 21:10
 */
public class StringLearn {

    /**
     * subString 这个方法返回值是输入值的包前不包后
     */
    @Test
    public void testSubStringMethod() {
        String source = "0123456789";
        String starget = source.substring(0, source.indexOf("3"));
        System.out.println(source.indexOf("3"));
        System.out.println(starget);

        Map<String,String> map = new HashMap<>();
        TypeVariable<? extends Class<? extends Map>>[] typeParameters = map.getClass().getTypeParameters();

        System.out.println(typeParameters.length);
    }

    @Test
    public void testGenericMethod() {
        Method[] methods = Function.class.getMethods();
        for (Method method:methods) {
            Type[] genericParameterTypes = method.getGenericParameterTypes();
            for (Type type:genericParameterTypes) {
                System.out.println(type);
            }
        }
    }

    @Test
    public void testn() {
        Method[] methods = SampleClass.class.getMethods();
        Type[] parameters = methods[1].getGenericParameterTypes();
        for (int i = 0; i < parameters.length; i++) {
            System.out.println(parameters[i]);
        }

        if(parameters[2] instanceof TypeVariable) {
            //ParameterizedType types = (ParameterizedType) parameters[2];
            //System.out.println(types.getActualTypeArguments());
            System.out.println(1111);
        }

        //System.out.println("------------------------------");
        //parameters = methods[2].getGenericParameterTypes();

        // 将类型向参数化类型转换
        //ParameterizedType t = (ParameterizedType)parameters[0];
        // 可以得到参数化类型的参数实例
        //Type actualTypeArgument = t.getActualTypeArguments()[0];
        //System.out.println(actualTypeArgument);

    }

    @Test
    public void testn1() {
        Function<Object, String> function = String::valueOf;
        System.out.println(function.apply(5));

    }

    public void testn3() {

    }

    public void testList(List<? super Integer> list) {
        list.add(5);
        //list.add(15.6);//编译异常
    }

    public void testList2(List<? extends Number> list) {
        //list.add(5);
    }

    // 测试list的toString方法
    @Test
    public void test5() {
        //List<String> list = new ArrayList<>();
        //list.add("a");
        //list.add("b");
        //list.add("c");
        //System.out.println("权限" + list);
        Date date = new java.sql.Date(1655744767321L);
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy:HH:mm:ss");
        String dateStr = dateFormat.format(date);
        System.out.println(dateStr);
    }

    @Test
    public void test6() {
        try {
            long start = System.currentTimeMillis();
            System.out.println(start);
            TimeUnit.MILLISECONDS.sleep(5000 - System.currentTimeMillis()%1000 );
            System.out.println(System.currentTimeMillis() - start);
        } catch (InterruptedException e) {

        }
    }

}

@Getter
class TestClone implements Cloneable {
    private String str;
    public TestClone(String str) {
        this.str = str;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        TestClone c1 = new TestClone("c1");
        System.out.println(c1.getStr());
        TestClone c2 = (TestClone) c1.clone();
        c1.setStr("c11");
        System.out.println(c2.getStr());
    }
}

class SampleClass {
    private String sampleField;

    public String getSampleField() {
        return sampleField;
    }

    public <T> void setSampleField(List<String> sampleField,String a,T T) {
        //this.sampleField = sampleField;
    }

    public void test(List<String> sampleField) {
        //this.sampleField = sampleField;
    }
}
